package com.gpt.article.service.impl;

import java.util.HashMap;
import java.util.List;

import com.gpt.article.domain.GptSpiderArticle;
import com.gpt.article.domain.GptSpiderArticleContent;
import com.gpt.article.mapper.GptGenArticleContentMapper;
import com.gpt.article.mapper.GptSpiderArticleMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.gpt.article.domain.GptGenArticleContent;
import com.gpt.article.mapper.GptGenArticleMapper;
import com.gpt.article.domain.GptGenArticle;
import com.gpt.article.service.IGptGenArticleService;
import org.springframework.util.ObjectUtils;

/**
 * 文章生成Service业务层处理
 * 
 * @author zhuzi
 * @date 2023-10-24
 */
@Service
public class GptGenArticleServiceImpl implements IGptGenArticleService 
{
    @Autowired
    private GptGenArticleMapper gptGenArticleMapper;
    @Autowired
    private GptGenArticleContentMapper gptGenArticleContentMapper;
    @Autowired
    private GptSpiderArticleMapper gptSpiderArticleMapper;
    /**
     * 查询文章生成
     * 
     * @param id 文章生成主键
     * @return 文章生成
     */
    @Override
    public GptGenArticle selectGptGenArticleById(Long id)
    {
        return gptGenArticleMapper.selectGptGenArticleById(id);
    }

    /**
     * 查询文章生成列表
     * 
     * @param gptGenArticle 文章生成
     * @return 文章生成
     */
    @Override
    public List<GptGenArticle> selectGptGenArticleList(GptGenArticle gptGenArticle)
    {
        return gptGenArticleMapper.selectGptGenArticleList(gptGenArticle);
    }
    @Override
    public List<GptGenArticle> selectExportGptGenArticleList(GptGenArticle gptGenArticle)
    {
        if(ObjectUtils.isEmpty(gptGenArticle.getIds())){
            return gptGenArticleMapper.selectGptGenArticleList(gptGenArticle);
        }else{
            return gptGenArticleMapper.selectGptGenArticleListByPs(gptGenArticle);
        }

    }

    /**
     * 新增文章生成
     * 
     * @param gptGenArticle 文章生成
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGptGenArticle(GptGenArticle gptGenArticle)
    {
        gptGenArticle.setCreateTime(DateUtils.getNowDate());

        //插入
        if(StringUtils.isEmpty(gptGenArticle.getUrlMd5())){
            String md5pre = DateUtils.dateTimeNow()+"zhu" +gptGenArticle.getTitle();
            String md5 = Md5Utils.hash(md5pre);
            gptGenArticle.setUrlMd5(md5);
        }


        int rows = gptGenArticleMapper.insertGptGenArticle(gptGenArticle);

        if(StringUtils.isNotNull(gptGenArticle.getArtContent())){
            insertGptGenArticleContent(gptGenArticle);
        }


        return rows;
    }

    /**
     * 修改文章生成
     * 
     * @param gptGenArticle 文章生成
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGptGenArticle(GptGenArticle gptGenArticle)
    {
        gptGenArticle.setUpdateTime(DateUtils.getNowDate());
        String urlMd5[] = gptGenArticle.getUrlMd5().split(";");
        gptGenArticleMapper.deleteGptGenArticleContentByMd5s(urlMd5);
        insertGptGenArticleContent(gptGenArticle);
        return gptGenArticleMapper.updateGptGenArticle(gptGenArticle);
//        gptGenArticleMapper.deleteGptGenArticleContentById(gptGenArticle.getId());
//        insertGptGenArticleContent(gptGenArticle);
//        return gptGenArticleMapper.updateGptGenArticle(gptGenArticle);
    }

    /**
     * 批量删除文章生成
     * 
     * @param ids 需要删除的文章生成主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGptGenArticleByIds(Long[] ids)
    {

        gptGenArticleMapper.deleteGptGenArticleContentByIds(ids);
        //删除对应内容
        List<GptGenArticle> gptGenArticleList = gptGenArticleMapper.getSelectedArticles(ids);
        List<String> urlMd5s = new ArrayList<String>();
        for(GptGenArticle gptGenArticle:gptGenArticleList){
            urlMd5s.add(gptGenArticle.getUrlMd5());
        }
        String[] arr = new String[urlMd5s.size()];
        urlMd5s.toArray(arr);
        gptGenArticleMapper.deleteGptGenArticleContentByMd5s(arr);
        return gptGenArticleList.size();
    }

    /**
     * 删除文章生成信息
     * 
     * @param id 文章生成主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGptGenArticleById(Long id)
    {
        GptGenArticle genArticle = gptGenArticleMapper.selectGptGenArticleById(id);
        String md5Urls[] = genArticle.getUrlMd5().split(" ");
        gptGenArticleMapper.deleteGptGenArticleContentByMd5s(md5Urls);
        return gptGenArticleMapper.deleteGptGenArticleById(id);
    }

    /**
     * 获取原文和生成文章对比
     * @param urlMd5
     * @return
     */
    @Override
    public Map<String,Object> getAticleMap(String urlMd5){
        Map<String,Object> map = new HashMap<String,Object>();
        String md5Urls[] = urlMd5.split(" ");
        map =getContentMap(md5Urls);
        return map;
    }


    public Map<String,Object> getContentMap(String[] md5Urls){
        Map<String,Object> map = new HashMap<String,Object>();
        List<GptGenArticle> gptGenArticles = gptGenArticleMapper.selectGptGenArticleContentByMd5s(md5Urls);
        List<GptSpiderArticle> gptSpiderArticles = null;
        Long gsarticleId = 0l;
        if(gptGenArticles.size()==0){
            gptSpiderArticles = gptSpiderArticleMapper.selectGptSpiderArticleContentByMd5s(md5Urls);
            if(gptSpiderArticles.size()==0){
                return map;
            }else{
                GptSpiderArticle gptSpiderArticle = gptSpiderArticles.get(0);
                String originArtContent = gptSpiderArticle.getArtContent();
                String originTitle = gptSpiderArticle.getTitle();
//                String spiderContentMd5 = gptSpiderArticle.getUrlMd5();
                map.put("origin",originArtContent);
                map.put("originTitle",originTitle);
                map.put("artContent",null);

            }
        }else{
            GptGenArticle gptGenArticle = gptGenArticles.get(0);
             gsarticleId = gptGenArticle.getGenArticleId();
            GptSpiderArticle gptSpiderArticle =  gptSpiderArticleMapper.selectGptSpiderArticleById(gsarticleId);
            if(!ObjectUtils.isEmpty(gptSpiderArticle)){
                String originArtContent = gptSpiderArticle.getArtContent();
                String originTitle = gptSpiderArticle.getTitle();
                map.put("origin",originArtContent);
                map.put("originTitle",originTitle);
            }
            String genArtContent = gptGenArticle.getArtContent();
            String artTitle = gptGenArticle.getTitle();
            map.put("artContent",genArtContent);
            map.put("artTitle",artTitle);

        }
        return map;

    }



    /**
     * 新增生成文章内容信息
     * 
     * @param gptGenArticle 文章生成对象
     */
    public void insertGptGenArticleContent(GptGenArticle gptGenArticle)
    {

        String urlMd5 = gptGenArticle.getUrlMd5();
        String artContent = gptGenArticle.getArtContent();
        GptGenArticleContent gptGenArticleContent =new GptGenArticleContent();
        gptGenArticleContent.setUrlMd5(urlMd5);
        gptGenArticleContent.setArtContent(artContent);
        gptGenArticleContentMapper.insertGptGenArticleContent(gptGenArticleContent);
//        List<GptGenArticleContent> gptGenArticleContentList = gptGenArticle.getGptGenArticleContentList();
//        Long id = gptGenArticle.getId();
//        if (StringUtils.isNotNull(gptGenArticleContentList))
//        {
//            List<GptGenArticleContent> list = new ArrayList<GptGenArticleContent>();
//            for (GptGenArticleContent gptGenArticleContent : gptGenArticleContentList)
//            {
//                gptGenArticleContent.setId(id);
//                list.add(gptGenArticleContent);
//            }
//            if (list.size() > 0)
//            {
//                gptGenArticleMapper.batchGptGenArticleContent(list);
//            }
//        }
    }
}
